package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService ;
    @Autowired
    private IDepartmentService departmentService ;
    @Autowired
    private IRoleService roleService ;
    @RequestMapping("/list")
    @RequiredPermission(name="员工列表",expression = "employee:list")
    public String list(Model model , @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        //System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo );
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments );

        return "employee/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission(name="员工删除",expression = "employee:delete")
    public JsonResult delete(Long id){
        try {
            employeeService.delete(id);
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
    @RequestMapping("/input")
    @RequiredPermission(name="员工编辑",expression = "employee:input")
    public String input(Model model , Long id){
        model.addAttribute("roles", roleService.selectAll());
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments );
        if (id != null) {
            Employee employee = employeeService.selectOne(id);
            System.out.println(employee);
            model.addAttribute("employee",employee );
        }
        return "/employee/input";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiredPermission(name="员工保存或新增",expression = "employee:saveOrUpdate")
    public JsonResult saveOrUpdate(Employee employee, Long[] ids){
        Long id = employee.getId();
        if (id != null) {
            employeeService.update(employee,ids);
        }else{
            employeeService.insert(employee,ids);
        }
        return new JsonResult();
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
    public JsonResult updatePassword(String oldPassword , String newPassword){
        System.out.println(1);
        try {
            if (StringUtils.hasText(newPassword)) {
                employeeService.updateNewPassword(oldPassword,newPassword);
            }
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPassword(String newPassword,Long id){
        try {
            if (StringUtils.hasText(newPassword)) {
                employeeService.resetPassword(newPassword,id);
            }
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name,Long id){
        HashMap map = new HashMap() ;
       if(id==null){
           Employee employee  = employeeService.selectByName(name);
           map.put("valid",employee==null );
       }else{
           Employee employee = employeeService.selectOne(id);
           if(employee.getName().equals(name)){
               map.put("valid",true );
           }else {
               Employee employee1  = employeeService.selectByName(name);
               map.put("valid",employee1==null );
           }
       }
       return map ;
    }

    @RequestMapping("/exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee_import.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls();
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult();
    }

    @RequestMapping("/forbidden")
    @ResponseBody
    public JsonResult forbiddenAccount(Long id) {
        Employee employee = employeeService.selectOne(id);
        if(employee.getName().equals("admin")){
            return new JsonResult(false,"亲,您是老大,不能禁自己哦");
        }
        boolean status = employee.isStatus();
        employee.setStatus(!status);
        employeeService.update(employee,null );
        return new JsonResult();
    }

}
